/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.zyao89.view.zloading.adapter;

import com.zyao89.view.zloading.ResourceTable;
import com.zyao89.view.zloading.ZLoadingView;
import com.zyao89.view.zloading.Z_TYPE;

import ohos.aafwk.ability.AbilitySlice;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.BaseItemProvider;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.DependentLayout;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;

import java.security.SecureRandom;
import java.util.Collections;
import java.util.List;

/**
 * 功能描述
 *
 * @author jjy
 * @since 2021-03-22
 */
public class ShowViewAdapter extends BaseItemProvider implements Component.ClickedListener {
    private static final int COLOR_NUM = 255;
    private static final int CLO_NUM = 3;
    private static final int INDEX_TWO = 2;
    private static final String TAG = "Zloding ShowViewAdapter";
    private List<Integer> lines;
    private AbilitySlice slice;

    private Z_TYPE[] types;
    private ZLoadingView mLoadingViewOne;
    private ZLoadingView mLoadingViewTwo;
    private ZLoadingView mLoadingViewThree;
    private DependentLayout dependentLayout;

    /**
     * 方法描述
     *
     * @param lines
     * @param slice
     */
    public ShowViewAdapter(List<Integer> lines, AbilitySlice slice) {
        this.lines = lines;
        this.slice = slice;
    }

    public void setTypes(Z_TYPE[] types) {
        this.types = types.clone();
    }

    @Override
    public int getCount() {
        return lines == null ? 0 : lines.size();
    }

    @Override
    public Object getItem(int position) {
        if (lines != null && position >= 0 && position < lines.size()) {
            return lines.get(position);
        }
        return Collections.emptyEnumeration();
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public Component getComponent(int position, Component convertComponent, ComponentContainer componentContainer) {
        final Component cpt;
        if (convertComponent == null) {
            cpt = LayoutScatter.getInstance(slice).parse(ResourceTable.Layout_ability_show_item,
                    null, false);
        } else {
            cpt = convertComponent;
        }
        mLoadingViewOne = (ZLoadingView) cpt.findComponentById(ResourceTable.Id_loadingView_1);
        mLoadingViewTwo = (ZLoadingView) cpt.findComponentById(ResourceTable.Id_loadingView_2);
        mLoadingViewThree = (ZLoadingView) cpt.findComponentById(ResourceTable.Id_loadingView_3);
        mLoadingViewOne.setBindStateChangedListener(mLoadingViewOne);
        mLoadingViewTwo.setBindStateChangedListener(mLoadingViewTwo);
        mLoadingViewThree.setBindStateChangedListener(mLoadingViewThree);
        DependentLayout directionalLayoutOne =
                (DependentLayout)cpt.findComponentById(ResourceTable.Id_dependentlayout1);
        setBackgroundColor(directionalLayoutOne);
        directionalLayoutOne.setClickedListener(this::onClick);
        DependentLayout directionalLayoutTwo =
                (DependentLayout)cpt.findComponentById(ResourceTable.Id_dependentlayout2);
        setBackgroundColor(directionalLayoutTwo);
        directionalLayoutTwo.setClickedListener(this::onClick);
        DependentLayout directionalLayoutThree =
                (DependentLayout)cpt.findComponentById(ResourceTable.Id_dependentlayout3);
        setBackgroundColor(directionalLayoutThree);
        directionalLayoutThree.setClickedListener(this::onClick);
        for (int index = 0; index < CLO_NUM; index++) {
            Z_TYPE type = types[position * CLO_NUM + index];
            if (index == 0) {
                initBuilder(mLoadingViewOne, type.ordinal());
            } else if (index == 1) {
                initBuilder(mLoadingViewTwo, type.ordinal());
            } else if (index == INDEX_TWO) {
                initBuilder(mLoadingViewThree, type.ordinal());
            }
        }
        return cpt;
    }

    private void initBuilder(ZLoadingView zloadingView, int type) {
        zloadingView.setLoadingBuilder(Z_TYPE.values()[type]);
    }

    private void setBackgroundColor(Component component) {
        int rgb = Color.rgb(new SecureRandom().nextInt(COLOR_NUM), new SecureRandom().nextInt(COLOR_NUM),
                new SecureRandom().nextInt(COLOR_NUM));
        ShapeElement element = new ShapeElement();
        element.setRgbColor(RgbColor.fromArgbInt(rgb));
        component.setBackground(element);
    }

    @Override
    public void onClick(Component component) {
        dependentLayout = (DependentLayout) component;
        ZLoadingView mzLoadingView = (ZLoadingView)dependentLayout.getComponentAt(0);
        if (mzLoadingView.isVisiable) {
            mzLoadingView.setVisibility(Component.VISIBLE);
            mzLoadingView.startAnimation();
            mzLoadingView.isVisiable = false;
        } else {
            mzLoadingView.stopAnimation();
            mzLoadingView.setVisibility(Component.HIDE);
            mzLoadingView.isVisiable = true;
        }
    }
}
